840D - Destiny - CodeForces Solution


data structures probabilities *2500

Please click on ads to support us..

C++ Code:

// v.resize(unique(all(v)) - v.begin());
// - That's what happens when you sit behind a desk.
// You forget things.
// Like the weight in the hand of a gun that's loaded and one that's not. -
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define fast_io ios::sync_with_stdio(false);cin.tie(NULL);
#define file_io freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define FOR(i,k,n) for(int i = k; i < n; ++ i)
#define debf cout<<"(0-0)\n";
#define all(x) x.begin(), x.end()
#define dec(x) cout << fixed << setprecision(x);
#define pf push_front
#define ppf pop_front
#define dash " ------- "
#define what(x) cerr << #x << " is " << x << endl;
#define eb emplace_back
//#define int short int
//#define int long long
#define sz(s) (int) (s.size())
#define fl cout.flush()

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;
typedef unsigned long long ull;
typedef long double ld;

template <class T> using max_heap = priority_queue <T, vector <T>, less <T> >;
template <class T> using min_heap = priority_queue <T, vector <T>, greater <T> >;

constexpr int MOD = 1e9 + 7, N = 3e5 + 8, M = 1e4 + 8, INF = 1e9 + 8, LGN = 20, mod = 998244353;
constexpr long double eps = 1e-8, pi = 3.14159265359;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, q, a[N], vis[N], rn[200];
vector <int> tmp;
vector <int> v[N];

int main(){
  fast_io;
  cin >> n >> q;
  FOR (i, 1, n+1){
    cin >> a[i];
    v[a[i]].pb(i);
  }
  FOR (i, 0, 70){
    rn[i] = rng() % MOD;
  }
  while (q --){
    int l, r, k;
    cin >> l >> r >> k;
    int ans = INF;
    int cnst = (r - l + 1) / k;
    for (int i = 0; i < 70; ++ i){
      int x = a[rn[i] % (r-l+1) + l];
      if (x >= ans || vis[x] || v[x].size() <= (r - l + 1) / k){
        continue;
      }
      tmp.pb(x);
      vis[x] = 1;
      int sz = upper_bound(all(v[x]), r) - lower_bound(all(v[x]), l);
      if (sz > cnst){
        ans = x;
      }
    }
    cout << (ans == INF ? -1 : ans) << '\n';
    for (int i : tmp){
      vis[i] = 0;
    }
    tmp.clear();
  }
  return 0;
}

// Yesterday is history
// Tomorrow is a mystery
// but today is a gift
// That is why it is called the present


Comments

Submit
0 Comments
More Questions

946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary